package com.diudiutechnology.www.yadiudiu.util.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyApplication;
import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.xutils.common.Callback;

/**
 * Created by Administrator on 2017/9/19.
 */

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, MyApplication.WX_APPID, false);
        //将你收到的intent和实现IWXAPIEventHandler接口的对象传递给handleIntent方法
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
        finish();
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp baseResp) {
        String result = "";
        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                switch (baseResp.getType()) {
                    case 1:
                        //拿到了微信返回的code,立马再去请求access_token
                        String code = ((SendAuth.Resp) baseResp).code;

                        result = "发送成功";
                        //就在这个地方，用网络库什么的或者自己封的网络api，发请求去咯，注意是get请求

                        getWeChatlanding(code);
                        break;

                    case 2:
                        result = "微信分享成功";
                        break;
                }


                ToastUtils.show(this, result);
                finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                result = "发送取消";
                ToastUtils.show(this, result);
                finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                result = "发送被拒绝";
                ToastUtils.show(this, result);
                finish();
                break;
            default:
                result = "发送返回";
                ToastUtils.show(this, result);
                finish();
                break;
        }
    }

    private void getWeChatlanding(String code) {
        String data="https://api.weixin.qq.com/sns/oauth2/access_token?appid="+MyApplication.WX_APPID+"&secret="+MyApplication.APPSECRET+"&code="
                +code+"&grant_type=authorization_code";
        HttpClient.WeChatlanding(data, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println("stringmd5" + result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
